﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace h2100
{
    /// <summary>
    /// Bảng điều khiển của game: thông tin, lệnh...
    /// </summary>
    public class hControlPanel:hWindow
    {
        private hText _console;        
        private String _message = "";
        private hButton _btnMenu;
        private hButton _btnEndTurn;     
        private hGlobal.Command _command = hGlobal.Command.Undefined;
        private string _selObjName = "";
        private hActionManager _actionmanager;
        private hFogOfWar _fogOfwar;
        Texture2D txtOverlay;


        public hActionManager ActionManager
        {
            get { return _actionmanager; }
            set { _actionmanager = value; }
        }

        public string SelectedObjectName
        {
            get { return _selObjName; }
            set { _selObjName = value; }
        }

        private string _tooltipText = "";

        public string TooltipText
        {
            get { return _tooltipText; }
            set { _tooltipText = value; }
        }

        public hGlobal.Command Command
        {
        	get {return _command;}
            set { _command = value; }
        }
        
        public hText Console 
		{
			set { _console = value; }
            get { return _console; }
		}
        
        public String Message
        {
            get { return _message; }
            set { _message = value; }
        }

        private hMiniMap _miniMap;

        public hControlPanel(ContentManager content, hText console, hParams args)
        {
            _fogOfwar = new hFogOfWar(content);            
            txtOverlay = content.Load<Texture2D>(hGlobal.PathGui + "InfoPanel_overlay");

            String[] txt = { hGlobal.PathGui + hGlobal.ControlPanelTexture };
            create1SpriteObject(content, txt);
            X = hGlobal.ControlPanelX;
            Y = hGlobal.ControlPanelY;
            Width = hGlobal.ControlPanelWidth;
            Height = hGlobal.ControlPanelHeight;

            Console = console;
            
            _btnMenu = new hButton(content,hGlobal.ButtonMenuX,hGlobal.ButtonMenuY);
            _btnMenu.Text = "Menu";
            _btnMenu.Command = hGlobal.Command.Pause;
            _btnMenu.Console=console;
            
            _btnEndTurn = new hButton(content,hGlobal.ButtonEndTurnX, hGlobal.ButtonEndTurnY);
            _btnEndTurn.Text = "End turn";
            _btnEndTurn.Command = hGlobal.Command.EndTurn;
            _btnEndTurn.Console = console;

            _miniMap = new hMiniMap(content, args);
        }
        private int[] _subActions = new int[9];

        public new void update(hParams args, GameTime gameTime)
        {
            _fogOfwar.update(args);
            _miniMap.update(args, gameTime);

            TooltipText = "";
            SelectedObjectName = "";
            //if (args.SelectedStructure != null)
            //{
            //    SelectedObjectName = args.SelectedStructure.Name;
            //}
            //if (args.SelectedUnit != null)
            //{
            //    SelectedObjectName = args.SelectedUnit.Name;
            //}

            this.Command = hGlobal.Command.Undefined;
            base.update(args, gameTime);

            // nút menu
            _btnMenu.update(args, gameTime);
            if (_btnMenu.Selected)
            {
                this._command = _btnMenu.Command;
            }

            switch (args.GameTurn)
            {
                case hGlobal.Turn.Computer:
                    this.Command = hGlobal.Command.EndTurn;
                    break;

                case hGlobal.Turn.Player:
                    // nút endturn
                    _btnEndTurn.update(args, gameTime);
                    if (_btnEndTurn.Selected)
                    {
                        this._command = _btnEndTurn.Command;
                    }                    

                    switch (args.ControlPhase)
                    {
                        case hGlobal.ControlPhase.NoAction:
                            Message = "";
                            SelectedObjectName = "";
                            TooltipText = "";
                            break;
                        case hGlobal.ControlPhase.Action:                                                          
                            break;

                        case hGlobal.ControlPhase.SubAction:                           
                            break;

                        case hGlobal.ControlPhase.Selecting:                           
                            break;

                        default:
                            break;
                    }    
                    // hết switch (args.ControlPhase)

                    break;
                    // kết thúc case hGlobal.Turn.Player

                default:
                    break;
            }
            // hết switch (gameTurn)

            // Xử lý command
            switch (Command)
            {
                case hGlobal.Command.Pause:
                    args.GameState = hGlobal.GameState.Pause;
                    break;
                case hGlobal.Command.EndTurn:
                    switch (args.GameTurn)
                    {
                        case hGlobal.Turn.Player:
                            args.GameTurn = hGlobal.Turn.Computer;
                            break;
                        case hGlobal.Turn.Computer:
                            args.GameTurn = hGlobal.Turn.Player;
                            args.StartOfTurn = true;
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    break;
            }
            //Hết xử lý command

            _actionmanager.update(args, gameTime);
        }

        public void draw(GameTime gameTime, SpriteBatch spriteBatch, hParams args)
        {
            _fogOfwar.draw(spriteBatch, args);
            base.draw(gameTime, spriteBatch);
           // _console.print(gameTime, spriteBatch, 0, 480, 200, 20, "Production: " + args.EnergyProduction.ToString(), Color.Yellow, hGlobal.FontStyle.Bold);
            if (args.SelectedStructure != null)
            {
                _console.print(gameTime, spriteBatch, 200, 465, 350, 20, args.SelectedStructure.Name, Color.Magenta, hGlobal.FontStyle.BoldMedium);
                _console.print(gameTime, spriteBatch, 200, 485, 350, 20, "Power:" + args.SelectedStructure.Power.ToString(), Color.White, hGlobal.FontStyle.Regular);
                _console.print(gameTime, spriteBatch, 200, 505, 350, 20, "Energy:" + args.SelectedStructure.Energy.ToString(), Color.White, hGlobal.FontStyle.Regular);
            }
            if (args.SelectedUnit != null)
            {
                _console.print(gameTime, spriteBatch, 200, 465, 350, 20, args.SelectedUnit.Name, Color.Magenta, hGlobal.FontStyle.BoldMedium);
                _console.print(gameTime, spriteBatch, 200, 485, 350, 20, "Power:" + args.SelectedUnit.Power.ToString(), Color.White, hGlobal.FontStyle.Regular);
                _console.print(gameTime, spriteBatch, 200, 505, 350, 20, "Energy:" + args.SelectedUnit.Energy.ToString(), Color.White, hGlobal.FontStyle.Regular);
            }
            _console.print(gameTime, spriteBatch, 200, 560, 350, 20, TooltipText, Color.Magenta, hGlobal.FontStyle.Regular);
            _btnMenu.draw(gameTime, spriteBatch, 255,Color.GreenYellow,hGlobal.FontStyle.BoldMedium);
            _btnEndTurn.draw(gameTime, spriteBatch,255,Color.GreenYellow, hGlobal.FontStyle.BoldMedium);

            _miniMap.draw(spriteBatch);
            spriteBatch.Draw(txtOverlay, new Vector2(0, 450), Color.White);
            _console.print(gameTime, spriteBatch, 155, 455, 40, 20, args.Energy.ToString(), Color.Yellow, hGlobal.FontStyle.BoldMedium);
            _console.print(gameTime, spriteBatch, 155, 475, 40, 20, args.EnergyProduction.ToString("+#;#;0"), Color.CornflowerBlue, hGlobal.FontStyle.BoldMedium);
            _actionmanager.draw(args,gameTime, spriteBatch);
        }
    }
}
